﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;

namespace FlashCards.SL.FlashCardRepeater.ViewModels.Commands
{
    public class Command : ICommand
    {
        Action action;
        Func<bool> canExec;

        public Command(Action action)
        {
            this.action = action;
            canExec = null;
        }

        public Command(Action action, Func<bool> canExec)
        {
            this.action = action;
            this.canExec = canExec;
        }

        public bool CanExecute(object param)
        {
            return canExec != null ? canExec() : true;
        }

        public event EventHandler CanExecuteChanged;

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, null);
            }
        }

        public void Execute(object parameter)
        {
            if (action != null) action();
        }
    }

    public class CommandWithParam : ICommand
    {
        Action<object> action;
        Func<bool> canExec;

        public CommandWithParam(Action<object> action)
        {
            this.action = action;
            canExec = null;
        }

        public CommandWithParam(Action<object> action, Func<bool> canExec)
        {
            this.action = action;
            this.canExec = canExec;
        }

        public bool CanExecute(object parameter)
        {
            return canExec != null ? canExec() : true;
        }

        public event EventHandler CanExecuteChanged;

        public void RaiseCanExecuteChanged()
        {
            if (CanExecuteChanged != null)
            {
                CanExecuteChanged(this, null);
            }
        }

        public void Execute(object parameter)
        {
            if (action != null) action(parameter);
        }
    }
}
